﻿<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ui="clr-namespace:ProjectEuler.UI"
        xmlns:data="clr-namespace:ProjectEuler.Data"

        x:Name="This"
        x:Class="ProjectEuler.UI.MainWindow"
        x:ClassModifier="internal"
 
        ResizeMode="CanResizeWithGrip"
        Title="Project Euler by jachymko" 
        
        Height="480" 
        MinHeight="320"
        
        MinWidth="540"
        Width="690" 

        Icon="/App.ico"
        Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
        >
    
    <Window.CommandBindings>
        
        <CommandBinding Command="{x:Static ApplicationCommands.Close}" 
                        Executed="OnCloseExecuted"
                        CanExecute="OnCanExecuteClose"
        />

        <CommandBinding Command="{x:Static ui:Commands.Cancel}" 
                        Executed="OnCancelExecuted" 
                        CanExecute="OnCanExecuteCancel" 
        />

        <CommandBinding Command="{x:Static ui:Commands.CopyAnswer}" 
                        Executed="OnCopyAnswerExecuted"
                        CanExecute="OnCanExecuteCopyAnswer"
        />

        <CommandBinding Command="{x:Static ui:Commands.Run}" 
                        Executed="OnRunExecuted" 
                        CanExecute="OnCanExecuteRun" 
        />
        
    </Window.CommandBindings>
    
    <Window.Resources>
        <ui:ProblemInfoCollection x:Key="Problems" 
        />
        
        <ui:ProblemRun x:Key="Session" 
                       Completed="OnSessionCompleted" 
        />
    </Window.Resources>

    <Grid DataContext="{StaticResource Session}">
        
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ui:InfoBar Grid.Row="0">
            <ui:InfoBar.Visibility>
                <Binding Path="LastRunResult">
                    <Binding.Converter>
                        <data:ComposingConverter>
                            <data:NotNullConverter/>
                            <BooleanToVisibilityConverter/>
                        </data:ComposingConverter>
                    </Binding.Converter>
                </Binding>
            </ui:InfoBar.Visibility>
        </ui:InfoBar>

        <ListView Grid.Row="1" 
                  BorderThickness="0"
                  Cursor="{Binding IsExecutingOrStopping, Converter={StaticResource ConvertIsBusyToCursor}}"
                  FocusVisualStyle="{x:Null}"
                  ForceCursor="{Binding IsExecutingOrStopping}"
                  ItemsSource="{StaticResource Problems}" 
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                  SelectionMode="Single"
                  >
            
            <ListView.Resources>
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="HorizontalAlignment" Value="Center" />
                    <Setter Property="IsEnabled" Value="{Binding IsExecutingOrStopping, Source={StaticResource Session}, Converter={StaticResource NotConverter}}" />
                    <Setter Property="SnapsToDevicePixels" Value="True" />
                    <Setter Property="VerticalAlignment" Value="Center" />
                </Style>

                <Style TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="Padding" Value="4,2,2,2" />
                </Style>

                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                    <Setter Property="VerticalAlignment" Value="Center" />
                </Style>
                
            </ListView.Resources>
            
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Margin="3"
                                            IsItemsHost="True"
                                            Orientation="Vertical"
                    />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>

            <ListView.View>
                <GridView AllowsColumnReorder="False">
                    
                    <GridViewColumn Width="480">
                        
                        <GridViewColumn.Header>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="20" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>

                                <CheckBox Grid.Column="0" IsChecked="{Binding ShouldRunAll, Source={StaticResource Problems}}" />
                                <TextBlock Grid.Column="1" Text="Name" />
                            </Grid>
                        </GridViewColumn.Header>
                        
                        <GridViewColumn.CellTemplate>
                            <DataTemplate DataType="{x:Type ui:ProblemInfo}">
                                
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="20" />
                                        <ColumnDefinition />
                                    </Grid.ColumnDefinitions>

                                    <CheckBox Grid.Column="0" 
                                              IsChecked="{Binding IsSelected}" 
                                    />

                                    <TextBlock Grid.Column="1" 
                                               TextTrimming="None" 
                                               TextWrapping="Wrap">
                                        
                                        <TextBlock.Resources>
                                            <Style TargetType="{x:Type Hyperlink}">
                                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
                                                <Setter Property="IsEnabled" Value="{Binding IsExecutingOrStopping, Source={StaticResource Session}, Converter={StaticResource NotConverter}}" />
                                                <Setter Property="TextDecorations" Value="{x:Null}" />

                                                <Style.Triggers>
                                                    <MultiTrigger>
                                                        <MultiTrigger.Conditions>
                                                            <Condition Property="IsMouseOver" Value="True" />
                                                            <Condition Property="IsEnabled" Value="True" />
                                                        </MultiTrigger.Conditions>

                                                        <Setter Property="TextDecorations" Value="{x:Static TextDecorations.Underline}" />
                                                    </MultiTrigger>
                                                    
                                                    <MultiTrigger>
                                                        <MultiTrigger.Conditions>
                                                            <Condition Property="IsFocused" Value="True" />
                                                            <Condition Property="IsEnabled" Value="True" />
                                                        </MultiTrigger.Conditions>

                                                        <Setter Property="TextDecorations" Value="{x:Static TextDecorations.Underline}" />
                                                    </MultiTrigger>                       
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Resources>
                                        
                                        <Hyperlink NavigateUri="{Binding ID, Converter={StaticResource ConvertProblemNumberToUri}}"
                                                   ui:TextBlockService.BoundText="{Binding Title}"
                                        />
                                    </TextBlock>

                                </Grid>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="Result" Width="110">
                        
                        <GridViewColumn.CellTemplate>
                            <DataTemplate DataType="{x:Type ui:ProblemInfo}">
                                <Grid>
                                    
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="20" />
                                        <ColumnDefinition />
                                    </Grid.ColumnDefinitions>

                                    <Grid.ContextMenu>
                                        <ContextMenu>
                                            <MenuItem Command="{x:Static ui:Commands.CopyAnswer}" 
                                                      CommandParameter="{Binding}"
                                            />
                                        </ContextMenu>
                                    </Grid.ContextMenu>

                                    <Image Grid.Column="0" 
                                           Margin="3,0"
                                           Height="17"
                                           Source="{Binding CurrentStatus, Converter={StaticResource ConvertProblemStatusToImage}}"
                                           ToolTip="{Binding CurrentStatus}"
                                    />

                                    <TextBlock Grid.Column="1" 
                                               Style="{StaticResource {x:Static ui:TextBlockService.AutoToolTipStyleKey}}"
                                               Text="{Binding Result, Mode=OneWay}" 
                                               TextTrimming="CharacterEllipsis"
                                               TextWrapping="NoWrap"
                                    />

                                </Grid>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        
                    </GridViewColumn>

                    <GridViewColumn Header="Elapsed" Width="60">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate DataType="{x:Type ui:ProblemInfo}">
                                <TextBlock Text="{Binding Elapsed.TotalMilliseconds, StringFormat={}{0:0;&lt;1;&lt;1} ms}" 
                                           TextAlignment="Right" 
                                />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView>
            </ListView.View>
        </ListView>

        <Border Grid.Row="2">
            <Border.Background>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,0.05">
                    <LinearGradientBrush.GradientStops>
                        <GradientStop Offset="0" Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}" />
                        <GradientStop Offset="1" Color="{DynamicResource {x:Static SystemColors.ControlColorKey}}" />
                    </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>
            </Border.Background>

            <Grid>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                
                <TextBlock Grid.Column="0" 
                           VerticalAlignment="Center" Margin="10">
                    <Hyperlink NavigateUri="http://ProjectEuler.net/">http://ProjectEuler.net</Hyperlink>
                </TextBlock>
                
                <StackPanel Orientation="Horizontal" Grid.Column="2">

                    <StackPanel.Resources>
                        <Style TargetType="{x:Type Button}">
                            <Setter Property="Margin" Value="0,10,10,10" />
                            <Setter Property="Padding" Value="12,2" />
                            <Setter Property="Width" Value="75" />
                        </Style>
                    </StackPanel.Resources>

                    <Button Command="{x:Static ui:Commands.Run}"
                            Content="{Binding Text, Source={x:Static ui:Commands.Run}}"
                            IsDefault="True"
                    />
    
                    <Button Command="{x:Static ApplicationCommands.Close}"
                            Content="{Binding Text, Source={x:Static ApplicationCommands.Close}}"
                    />
                </StackPanel>

            </Grid>

        </Border>
    </Grid>
</Window>
